package com.mingtang.online;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.mingtang.online.activity.BaseActivity;
import com.mingtang.online.bean.Shouye;
import com.mingtang.online.changliang.Const;
import com.mingtang.online.fragment.FanliFragment;
import com.mingtang.online.fragment.FuLiFragment;
import com.mingtang.online.fragment.HomeFragment;
import com.mingtang.online.fragment.MeFragment;
import com.mingtang.online.fragment.YouHuiFragment;
import com.mingtang.online.http.MyOkhttp;
import com.mingtang.online.http.callback.StringNoDialogCallback;
import com.mingtang.online.http.request.BaseRequest;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/10/23.
 */

public class HideFragmentActivity extends BaseActivity {
    @BindView(R.id.content)
    FrameLayout content;
    @BindView(R.id.iv_home)
    ImageView ivHome;
    @BindView(R.id.tv_home)
    TextView tvHome;
    @BindView(R.id.ll_home)
    LinearLayout llHome;
    @BindView(R.id.iv_touzi)
    ImageView ivTouzi;
    @BindView(R.id.tv_touzi)
    TextView tvTouzi;
    @BindView(R.id.ll_touzi)
    LinearLayout llTouzi;
    @BindView(R.id.iv_me)
    ImageView ivMe;
    @BindView(R.id.tv_me)
    TextView tvMe;
    @BindView(R.id.ll_me)
    LinearLayout llMe;
    @BindView(R.id.iv_more)
    ImageView ivMore;
    @BindView(R.id.tv_more)
    TextView tvMore;
    @BindView(R.id.ll_more)
    LinearLayout llMore;
    @BindView(R.id.iv_fanli)
    ImageView ivFanli;
    @BindView(R.id.tv_fanli)
    TextView tvFanli;
    @BindView(R.id.ll_fanli)
    LinearLayout llFanli;

    private HomeFragment homeFragment;
    private YouHuiFragment touziFragment;
    //返利
    private FanliFragment fanliFragment;
    //福利
    private FuLiFragment fuLiFragment;
    private MeFragment mineFragment;

    private FragmentTransaction ft;
    List<Shouye.ContentBean> contentBeanList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hidefragment);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
//        ShouyeGuangGao();
        setSelect(0);
    }

    public void ShouyeGuangGao() {
        //轮播图
//	http://www.50210.com.cn/app/index.php?i=3&c=entry&do=getcdtype&m=mingtang


        MyOkhttp.post(Const.MODEL_URL)
                .tag(this)
                .params("i", "3")
                .params("c", "entry")
                .params("do", "getcdtype")
                .params("m", "mingtang")
                .execute(new StringNoDialogCallback() {
                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);


                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.d(s, "eeeeeeeeeeeeeeeeeeeee");
                        Log.d(s, "eeeeeeeeeeeeeeeeeeeee");
                       /* AccessToken accessToken = new AccessToken();
                        Gson gson = new Gson();
                        accessToken = gson.fromJson(s, AccessToken.class);*/
                        Shouye fenLeiBean = new Shouye();
                        Gson gson = new Gson();
                        fenLeiBean = gson.fromJson(s, Shouye.class);
                        contentBeanList = fenLeiBean.getContent();
                        try {


                        } catch (Exception e) {

                        }

                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);

                    }

                    @Override
                    public void onAfter(String s, Exception e) {
                        super.onAfter(s, e);

                    }
                });

    }

    private void setSelect(int i) {
        FragmentManager fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        reSetTab();
        hideFragment();
        switch (i) {
            case 0:
                //首页
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    ft.add(R.id.content, homeFragment);
                }
                ivHome.setImageResource(R.drawable.index_selected);
                tvHome.setTextColor(getResources().getColor(R.color.home_back_selected));
                ft.show(homeFragment);
                break;
            case 1:
                //投资
                if (touziFragment == null) {
                    touziFragment = new YouHuiFragment();
                    ft.add(R.id.content, touziFragment);
                }
                ivTouzi.setImageResource(R.drawable.youhuiselected);
                //  tvTouzi.setTextColor(UIUtils.getColor(R.color.home_back_selected));
                tvTouzi.setTextColor(getResources().getColor(R.color.home_back_selected));
                ft.show(touziFragment);
                break;
            case 2:
                //资产
                if (fanliFragment == null) {
                    fanliFragment = new FanliFragment();
                    ft.add(R.id.content, fanliFragment);
                }
                ivFanli.setImageResource(R.drawable.fanliselected);
                // tvMe.setTextColor(UIUtils.getColor(R.color.home_back_selected));
                tvFanli.setTextColor(getResources().getColor(R.color.home_back_selected));
                ft.show(fanliFragment);
                break;
            case 3:
                //更多
                if (fuLiFragment == null) {
                    fuLiFragment = new FuLiFragment();
                    ft.add(R.id.content, fuLiFragment);
                }
                ivMore.setImageResource(R.drawable.fuliselected);
                //  tvMore.setTextColor(UIUtils.getColor(R.color.home_back_selected));
                tvMore.setTextColor(getResources().getColor(R.color.home_back_selected));
                ft.show(fuLiFragment);
                break;
            case 4:
                //wod
                if (mineFragment == null) {
                    mineFragment = new MeFragment();
                    ft.add(R.id.content, mineFragment);
                }
                ivMe.setImageResource(R.drawable.mine_selected);
                //  tvMore.setTextColor(UIUtils.getColor(R.color.home_back_selected));
                tvMe.setTextColor(getResources().getColor(R.color.home_back_selected));
                ft.show(mineFragment);
                break;
        }
        ft.commit();
    }


    private void reSetTab() {
        ivHome.setImageResource(R.drawable.index_unseleced);
        ivTouzi.setImageResource(R.drawable.youhui_unselected);
        ivFanli.setImageResource(R.drawable.fanli_unselected);
        ivMore.setImageResource(R.drawable.fuli_unselected);
        ivMe.setImageResource(R.drawable.mine_unselected);
        tvHome.setTextColor(getResources().getColor(R.color.home_back_unselected));

        tvTouzi.setTextColor(getResources().getColor(R.color.home_back_unselected));
        tvFanli.setTextColor(getResources().getColor(R.color.home_back_unselected));
        tvMe.setTextColor(getResources().getColor(R.color.home_back_unselected));
        tvMore.setTextColor(getResources().getColor(R.color.home_back_unselected));
    }


    private void hideFragment() {
        if (homeFragment != null) {
            ft.hide(homeFragment);
        }
        if (touziFragment != null) {
            ft.hide(touziFragment);
        }
        if (fanliFragment != null) {
            ft.hide(fanliFragment);
        }
        if (fuLiFragment != null) {
            ft.hide(fuLiFragment);
        }
        if (mineFragment != null) {
            ft.hide(mineFragment);
        }

    }

    @OnClick({R.id.iv_home, R.id.tv_home, R.id.ll_home, R.id.iv_touzi, R.id.tv_touzi, R.id.ll_touzi, R.id.iv_me, R.id.tv_me, R.id.ll_me, R.id.iv_more, R.id.tv_more, R.id.ll_more, R.id.iv_fanli, R.id.tv_fanli, R.id.ll_fanli})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_home:
                setSelect(0);
                break;
            case R.id.tv_home:
                setSelect(0);
                break;
            case R.id.ll_home:
                setSelect(0);
                break;
            case R.id.iv_touzi:
                setSelect(1);
                break;
            case R.id.tv_touzi:
                setSelect(1);
                break;
            case R.id.ll_touzi:
                setSelect(1);
                break;
            case R.id.iv_fanli:
                setSelect(2);
                break;
            case R.id.tv_fanli:
                setSelect(2);
                break;
            case R.id.ll_fanli:
                setSelect(2);
                break;
            case R.id.iv_more:
                setSelect(3);
                break;
            case R.id.tv_more:
                setSelect(3);
                break;
            case R.id.ll_more:
                setSelect(3);
                break;

            case R.id.iv_me:
                setSelect(4);
                break;
            case R.id.tv_me:
                setSelect(4);
                break;
            case R.id.ll_me:
                setSelect(4);
                break;

        }
    }

    public void onEventMainThread(Integer type) {
        Log.e("---", "EventBus收到int:" + type);
        if (type == 0) {
            setSelect(1);
        } else if (type == 1) {
            setSelect(3);
        } else if (type == 2) {
            setSelect(4);

        } else if (type == 4) {
            setSelect(4);
        } else {
            setSelect(0);
        }


    }


}
